package com.todo.biz.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.biz.infrastructure.entity.condition.FeedbackCondition;
import com.todo.biz.infrastructure.entity.po.FeedbackPO;
import com.todo.biz.infrastructure.mapper.FeedbackMapper;
import com.todo.biz.infrastructure.repository.FeedbackRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Feedback)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("feedbackRepository")
public class FeedbackRepositoryImpl implements FeedbackRepository {

    public static final int MAX_POST_FEEDBACK_COUNT = 10;

    @Resource
    private FeedbackMapper feedbackMapper;


    @Override
    public PageResult<FeedbackPO> queryFeedbackByCondition(FeedbackCondition feedbackCondition) {
        PageResult<FeedbackPO> pageResult = new PageResult<>();
        if (feedbackCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(feedbackCondition.getPageIndex());
        pageResult.setPageSize(feedbackCondition.getPageSize());
        long count = queryCountByCondition(feedbackCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, feedbackCondition.getPageSize()));
        pageResult.setTotal(count);
        List<FeedbackPO> feedbackPOList = feedbackMapper.queryFeedbackPOByCondition(feedbackCondition);
        if (CollectionUtils.isEmpty(feedbackPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(feedbackPOList);
        pageResult.setCurrentIndex(feedbackPOList.get(0).getId());
        pageResult.setHasNext(feedbackPOList.size() >= feedbackCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(FeedbackCondition feedbackCondition) {
        if (feedbackCondition == null || feedbackCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return feedbackMapper.queryCountByCondition(feedbackCondition);
    }

    @Override
    public FeedbackPO queryFeedbackPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return feedbackMapper.queryFeedbackPOById(id);
    }

    @Override
    public Boolean insertFeedbackPO(FeedbackPO feedbackPO) {
        if (feedbackPO == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        List<FeedbackPO> feedbackList = feedbackMapper.queryFeedbackListByUserAndStatus(feedbackPO.getCreateUser(), 1);
        if (feedbackList.size() >= MAX_POST_FEEDBACK_COUNT) {
            throw new BizException(BizErrorCodeEnum.RELATION_DATA_EXCESSIVE);
        }
        return feedbackMapper.insertFeedbackPO(feedbackPO) > 0;
    }

    @Override
    public Boolean updateFeedbackPOById(FeedbackPO feedbackPO) {
        if (feedbackPO == null || feedbackPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return feedbackMapper.updateFeedbackPOById(feedbackPO) > 0;
    }

    @Override
    public Boolean removeFeedbackPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        FeedbackPO feedbackPO = queryFeedbackPOById(id);
        if (feedbackPO == null) {
            return true;
        }
        return feedbackMapper.removeFeedbackPOById(id) > 0;
    }

    @Override
    public Boolean updateStatusById(Long id, Integer oldStatus, Integer newStatus) {
        if (id == null || id < 0 || oldStatus == null || newStatus == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FeedbackPO feedbackPO = queryFeedbackPOById(id);
        if (feedbackPO == null || !feedbackPO.getStatus().equals(oldStatus)) {
            log.info("FeedbackRepository#updateStatusById 更新反馈信息状态失败，查询反馈记录,或者当前状态不对 id：【{}】feedbackPO：【{}】", id, JSONObject.toJSONString(feedbackPO));
            return false;
        }
        if (feedbackPO.getStatus().equals(newStatus)) {
            return true;
        }
        return feedbackMapper.updateStatusById(id, oldStatus, newStatus);
    }

    @Override
    public List<FeedbackPO> queryFeedbackListByUserAndStatus(Long createUser, Integer status) {
        if (createUser == null || status == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return feedbackMapper.queryFeedbackListByUserAndStatus(createUser, status);
    }

    @Override
    public Boolean removeFeedbackPOByIdList(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return true;
        }
        Boolean result = feedbackMapper.removeFeedbackPOByIdList(idList);
        return result;
    }
}
